/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  PersonInfoHuoDongActivity.java   
 * @Package com.jiyi.activity.zone   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-6-16 上午9:28:04   
 * @version V1.0     
 */
package com.jiyi.activity.zone;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jiyi.adapter.DividerItemDecoration;
import com.jiyi.adapter.recyclerview.CommonAdapter;
import com.jiyi.adapter.recyclerview.base.ViewHolder;
import com.jiyi.adapter.recyclerview.wrapper.EmptyWrapper;
import com.jiyi.adapter.recyclerview.wrapper.HeaderAndFooterWrapper;
import com.jiyi.bean.zone.Topic;
import com.jiyi.common.StringInterface;
import com.jiyi.common.VolleyRequest;
import com.jiyi.inteface.WebApi;
import com.swift.maintenance.R;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.maintenance.utils.UserInfoUtil;
import com.swift.utils.DeviceUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @ClassName: PersonInfoHuoDongActivity
 * @Description:TODO(这里用一句话描述这个类的作用)
 * @author: HYM
 * @date: 2017-6-16 上午9:28:04
 * 
 */
@SuppressLint("NewApi")
public class PersonInfoTopicFragment extends Fragment {

	@BindView(R.id.id_stickynavlayout_innerscrollview)
	RecyclerView mListView;
	private List<Topic> mTopicDatas = new ArrayList<Topic>();
	EmptyWrapper adapter;
	int headNum =0;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_tab, container, false);
		ButterKnife.bind(this, view);
		getParams();
		// 设置布局管理器
		mListView.setLayoutManager(new LinearLayoutManager(getActivity()));
		// 添加分割线
		mListView.addItemDecoration(new DividerItemDecoration(getActivity(),
				DividerItemDecoration.VERTICAL_LIST, getResources()
						.getDrawable(R.drawable.drvice_10)));
		// 初始化适配器
		CommonAdapter mCommonAdapter = new CommonAdapter<Topic>(getActivity(),
				R.layout.item_jiyouxiu, mTopicDatas) {
			@Override
			protected void convert(ViewHolder holder, final Topic t,
					final int position) {
				holder.setText(R.id.tvUserName, t.getUser_name());
				holder.setText(R.id.tvCreateDate, t.getCreate_time());
				if (TextUtils.isEmpty(t.getContent())) {
					holder.setVisible(R.id.tvContent, false);
				} else {
					holder.setTopicText(R.id.tvContent, t);
				}

				holder.setText(R.id.tvGoodNum, t.getLikeNum() + "");
				holder.setImage(R.id.ivLogo, t.getUser_logo());

				if (t.getImage() == null || t.getImage().size() <= 0) {
					holder.setVisible(R.id.gvXiu, false);
				} else {
					holder.setVisible(R.id.gvXiu, true);
					holder.setAdapter(R.id.gvXiu, t.getImage());
				}
				
				if (t.getLikeNum() > 0) {
					holder.setVisible(R.id.tvGoodNum, true);
					holder.setText(R.id.tvGoodNum, t.getLikeNum()+"");
				}
				else {
					holder.setVisible(R.id.tvGoodNum, false);
				}
				
				if (t.getIs_praise()) {
					holder.setImageResource(R.id.ivGood, R.mipmap.icon_praise_on);
					holder.setText(R.id.tvGoodNum, t.getLikeNum()+"");
				}
				else {
					holder.setImageResource(R.id.ivGood, R.mipmap.icon_praise_normal);
				}
				
				holder.setOnClickListener(R.id.rlPraise, new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						//为什么减去headNum,因为adapter加了一个headview，所以position会多一个出来，点击的时候需要扣除添加的headview个数
						praise(t.getId(), position-headNum,t.getIs_praise());
					}
				});
			}
		};
		// 构建一个头部与脚部装饰适配器，传入之前的适配器
		HeaderAndFooterWrapper headAdapter = new HeaderAndFooterWrapper(
				mCommonAdapter);
		View drivier = new View(getActivity());
		// 标志该对象是头部，不需要分割线，否则头部也会有分割线
		drivier.setTag("head");
		headNum ++;
		LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,
				(int) (10 * DeviceUtil.getInstance().getBiLi()));
		drivier.setLayoutParams(params);
		drivier.setBackground(getActivity().getResources().getDrawable(
				R.drawable.drvice_10));
		headAdapter.addHeaderView(drivier);
		
		if (mTopicDatas.size() >0) {
			adapter = new EmptyWrapper(headAdapter);
		}
		else {
			adapter = new EmptyWrapper(mCommonAdapter);
		}
		
		View emptyView = inflater.inflate(R.layout.empty_view_text_and_image, container, false);
		ImageView ivEmptyTip = (ImageView) emptyView.findViewById(R.id.ivEmptyTip);
		TextView tvEmptyTip = (TextView) emptyView.findViewById(R.id.tvEmptyTip);
		ivEmptyTip.setImageResource(R.mipmap.tip_empty_xiu);
		tvEmptyTip.setText("TA 还没有参与任何话题哦 ~");
		adapter.setEmptyView(emptyView);
		
		// recycleview设置适配器
		mListView.setAdapter(adapter);
		adapter.notifyDataSetChanged();
		return view;

	}

	/**
	 * @author: HYM
	 * @Title: getParams
	 * @Description: TODO(这里用一句话描述这个方法的作用) void
	 * @date: 2017-6-22
	 */
	private void getParams() {
		String datas = (String) getArguments().get("datas");
		mTopicDatas = new Gson().fromJson(datas,
				new TypeToken<List<Topic>>() {
				}.getType());
	}
	
	void praise(String id,final int position,Boolean flag) {
		Map<String, String> params = new HashMap<String, String>();
		params.put(AppConfig.ACCESS_TOKEN, UserInfoUtil.getCurrentUserAccessToken());
		params.put("id", id);
		params.put("flag", flag.equals(true) ? "1" : "0");
		VolleyRequest.StringRequestPost(getContext(), WebApi.MAIN_TOPIC_PRAISE, "", params,
				new StringInterface(getContext()) {

					@Override
					public void stringSuccess(String result) {
						try {
							JSONObject obj = new JSONObject(result);
							String status = obj.getString("status");
							String info = obj.getString("info");
							if (status.equals("200")) {
								ToastUtil.getInstances().showMsg(info);
								String data = obj.getString("data");
								JSONObject dataJson = new JSONObject(data);
								String praiseNum = dataJson.getString("likeNum");
								String flag = dataJson.getString("flag");
								mTopicDatas.get(position).setIs_praise(flag.equals("1") ? true : false);
								mTopicDatas.get(position).setLikeNum(Integer.parseInt(praiseNum));
								//获取返回 的点赞数和是否点赞标志，然后局部刷新
//								adapter.notifyItemChanged(position,mTopicDatas.size());
								adapter.notifyDataSetChanged();
								
							} else {
								ToastUtil.getInstances().showMsg(info);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						} finally {
						}
					}

					@Override
					public void stringError(VolleyError error) {
						ToastUtil.getInstances().showMsg(R.string.err_http_response);
					}
				});
	}
}
